<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="学生姓名" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入学生姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年级" prop="gradeName">
        <el-input
          v-model="queryParams.gradeName"
          placeholder="请输入年级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="班级" prop="className">
        <el-input
          v-model="queryParams.className"
          placeholder="请输入班级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['com/ruoyi/business:leaves:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['com/ruoyi/business:leaves:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:leaves:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['com/ruoyi/business:leaves:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border stripe :header-cell-style="rowClass" v-loading="loading" :data="leavesList" @selection-change="handleSelectionChange">
      <el-table-column :resizable="false" type="selection" width="55" align="center" />
      <el-table-column :resizable="false" label="学生姓名" align="center" prop="studentName" />
      <el-table-column :resizable="false" label="年级" align="center" prop="gradeName" />
      <el-table-column :resizable="false" label="班级" align="center" prop="className" />
      <el-table-column :resizable="false" label="请假原因" align="center" prop="reason" />
      <el-table-column :resizable="false" label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :resizable="false" label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="状态：0=待审批, 1=审批通过, 2=审批拒绝" align="center" prop="status" > -->
      <el-table-column :resizable="false" label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <div v-if="scope.row.status==0" class="flex gap-2">
            <el-tag type="info" effect="dark" >
              待审核
            </el-tag>
          </div>
          <div v-else-if="scope.row.status==1" class="flex gap-2">
            <el-tag type="success" effect="dark" >
              通过
            </el-tag>
          </div>
          <div v-else class="flex gap-2">
            <el-tag type="danger" effect="dark" >
              拒绝
            </el-tag>
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="逻辑删除标记，0=未删除，1=已删除" align="center" prop="isDeleted" /> -->
      <el-table-column :resizable="false" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['com/ruoyi/business:leaves:edit']"
          >修改</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:leaves:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document-remove"
            @click="leaveRecord(scope.row)"
            v-hasPermi="['business:leaves:approval']"
          >请假审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

     <!-- 请假审批对话框 -->
     <el-dialog :title="title" :visible.sync="leaveRecordOpen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="请假原因" prop="reason">
          <el-input disabled v-model="form.reason" placeholder="请输入请假原因" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-input disabled v-model="form.startTime"></el-input>
          <!-- <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开始时间">
          </el-date-picker> -->
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-input disabled v-model="form.endTime"></el-input>
          <!-- <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结束时间">
          </el-date-picker> -->
        </el-form-item>
        <el-form-item label="学校审批状态"  prop="schoolApprovalStatus">
          <el-radio-group v-model="schoolApprovalStatus">
            <el-radio :label="1">通过</el-radio>
            <el-radio :label="2">不通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="学校审批意见" prop="schoolComment">
          <el-input v-model="form.schoolComment" placeholder="请输入学校审批意见" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改请假对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学生ID" prop="studentId">
          <el-input v-model="form.studentId" placeholder="请输入学生ID" />
        </el-form-item>
        <el-form-item label="提交请假的id" prop="parentId">
          <el-input v-model="form.parentId" placeholder="请输入提交请假的id" />
        </el-form-item>
        <el-form-item label="学校ID" prop="schoolId">
          <el-input v-model="form.schoolId" placeholder="请输入学校ID" />
        </el-form-item>
        <el-form-item label="请假原因" prop="reason">
          <el-input v-model="form.reason" placeholder="请输入请假原因" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="创建时间" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="updatedAt">
          <el-date-picker clearable
            v-model="form.updatedAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="逻辑删除标记，0=未删除，1=已删除" prop="isDeleted">
          <el-input v-model="form.isDeleted" placeholder="请输入逻辑删除标记，0=未删除，1=已删除" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLeaves, getLeaves, delLeaves, addLeaves, updateLeaves, getLeavetotal } from "@/api/leaveSystem/leaveList";
import { addleaveRecord } from "@/api/leaveSystem/leaveRecord";
export default {
  name: "Leaves",
  data() {
    return {
      //审批状态
      schoolApprovalStatus: 1,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      //选中学生姓名数组
      studentNames:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 请假表格数据
      leavesList: [],
      // 弹出层标题
      title: "",
      //请假审批是否显示弹出层
      leaveRecordOpen:false,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentId: null,
        parentId: null,
        schoolId: null,
        reason: null,
        startTime: null,
        endTime: null,
        status: null,
        createdAt: null,
        updatedAt: null,
        isDeleted: null,
        studentName:null,
        className:null,
        gradeName:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        studentId: [
          { required: true, message: "学生ID不能为空", trigger: "blur" }
        ],
        parentId: [
          { required: true, message: "提交请假的id不能为空", trigger: "blur" }
        ],
        schoolId: [
          { required: true, message: "学校ID不能为空", trigger: "blur" }
        ],
        // reason: [
        //   { required: true, message: "请假原因不能为空", trigger: "blur" }
        // ],
        // startTime: [
        //   { required: true, message: "开始时间不能为空", trigger: "blur" }
        // ],
        // endTime: [
        //   { required: true, message: "结束时间不能为空", trigger: "blur" }
        // ],
        status: [
          { required: true, message: "状态：0=待审批, 1=审批通过, 2=审批拒绝不能为空", trigger: "change" }
        ],
        schoolComment:[
        { required: true, message: "意见不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //设置table标题样式
    rowClass ({ row, rowIndex }) {
      return 'font-size: 15px; background-color: #FAFAFA;'
    },
    /** 查询请假列表 */
    getList() {
      this.loading = true;
      this.queryParams.status = 0;
      listLeaves(this.queryParams).then(response => {
        this.leavesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.leaveRecordOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        studentId: null,
        parentId: null,
        schoolId: null,
        reason: null,
        startTime: null,
        endTime: null,
        status: null,
        createdAt: null,
        updatedAt: null,
        isDeleted: null,
        studentName:null,
        className:null,
        gradeName:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.studentNames=selection.map(item => item.studentName)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加请假";
    },
    // 请假审批
    leaveRecord(row){
      this.reset();
      // this.leaveRecordOpen = true;
      console.log("========row.leaveId=",row)
      getLeaves(row.id).then(response => {
        this.form = response.data;
        this.leaveRecordOpen = true;
        this.title = "请假审批";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLeaves(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改请假";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.leaveRecordOpen){
            this.form.leaveId = this.form.id
            this.form.schoolApprovalStatus=this.schoolApprovalStatus
            addleaveRecord(this.form).then(response => {
              this.$modal.msgSuccess("审核成功");
              this.leaveRecordOpen = false;
              this.schoolApprovalStatus=1
              this.getList();
            });
            return
          }
          if (this.form.id != null) {
            updateLeaves(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLeaves(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log(row)
      const ids = row.id || this.ids;
      const name=row.studentName || this.studentNames
      this.$modal.confirm('是否确认删除姓名为"' + name + '"的数据项？').then(function() {
        return delLeaves(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/leaves/export', {
        ...this.queryParams
      }, `leaves_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped lang="scss">
.topBox{
  height: 150px;
  display: flex;
  align-items: center;
  .circle-stat {
    position: relative;
    display: flex;
    flex-direction: column;
    justify-content: center;
    width: 120px;
    height: 120px;
    text-align: center;
    border-radius: 50%;
    background: linear-gradient(45deg, #ff9966, #ff5e62); /* 渐变背景 */
    color: white; /* 文本颜色 */
    font-size: 20px; /* 字体大小 */
    font-weight: bold; /* 字体加粗 */
    margin-right: 30px; /* 右边距 */
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.3); /* 阴影效果 */
  }

  .circle-stat::before {
    content: "";
    position: absolute;
    top: -10px;
    left: -10px;
    right: -10px;
    bottom: -10px;
    border-radius: 50%;
    background: inherit;
    opacity: 0.3;
    z-index: -1;
  }
}

</style>
